<?php
//-------------------------------------------------------------------------------------------
//----------------------------- DETAIL DE LA FACTURE  --------------------------------------
//-------------------------------------------------------------------------------------------
//------------------------------ Affichage de l'équipement
?>
<p></p>
<?php
if (!empty($tab_equipement)) {
    ?> 
    <table width="500" align="left" >
        <tr >
            <td width="290" align="left"><b>Equipement</b></td>
            <td width="50" align="center"><b>Quantité</b></td>
            <td width="80" align="center"><b>Prix Unitaire</b></td>
            <td width="80" align="center"><b>Montant € HT</b></td>
        </tr>
        <?php
        for ($i = 0; $i < count($tab_equipement); $i++) {
            ?>        
            <tr>
                <td><?php echo $tab_equipement[$i]['nom']; ?>
                    <?php if ($tab_equipement[$i]['financement'] > 1) { ?><br />Financement sur <?php echo $tab_equipement[$i]['financement']; ?> mois.
                        <br />Acquisition le <?php echo date("d/m/Y", strtotime($tab_equipement[$i]['date_achat'])); ?>
                    <?php } ?></td>
                <td align="center"><?php echo $tab_equipement[$i]['quantite']; ?></td>
                <td align="center"><?php echo $tab_equipement[$i]['prix']; ?></td>
                <td align="right"><?php echo number_format($tab_equipement[$i]['prix_finance_total'], 2); ?></td>
            </tr>
            <?php
        }
        ?>
        <tr>
            <td colspan="3">TOTAL</td>
            <td align="right"><?php echo number_format($total_equipement, 2); ?></td>
        </tr>
    </table>
    <hr />
    <?php
}
?>
<?php
//------------------------------ Affichage des forfaits
// boucle par le ndi
foreach ($tab_forfait as $ndi => $data) {
    ?>
    <table width="560" align="left" >
        <tr >
            <td width="260" align="left"><b>Services et produits</b></td>
            <td width="100" align="left"><b>Durée</b></td>
            <td width="100" align="left"><b>Hors Forfait</b></td>
            <td width="100" align="center"><b>Montant € HT</b></td>
        </tr>
        <tr>
            <td>
                Pour la ligne <?php echo $ndi; ?>
            </td>
            <td></td><td></td>
            <td align="center"></td>
        </tr>
        <?php
        //Affichage des consos du ndi
        if ($tab_consos[$ndi]['duree'] != '0' and $tab_consos[$ndi]['fax_to_mail'] == '0') {
            ?>

            <?php
            if ($tab_consos[$ndi]['duree_fixe'] > '0') {
                ?>
                <tr >
                    <td width="260" align="left">Fixe</td>
                    <td width="100" align="left"><?php echo $tab_consos[$ndi]['duree_fixe']; ?></td>
                    <td width="100" align="left"><?php echo $tab_consos[$ndi]['hors_forfait_fixe']; ?></td>
                    <td width="100" align="right"><?php echo number_format($tab_consos[$ndi]['cout_reel_fixe'], 2); ?></td>
                </tr>
                <?php
            }
            if ($tab_consos[$ndi]['duree_mobile'] > '0') {
                ?>
                <tr >
                    <td width="260" align="left">Mobile</td>
                    <td width="100" align="left"><?php echo $tab_consos[$ndi]['duree_mobile']; ?></td>
                    <td width="100" align="left"><?php echo $tab_consos[$ndi]['hors_forfait_mobile']; ?></td>
                    <td width="100" align="right"><?php echo number_format($tab_consos[$ndi]['cout_reel_mobile'], 2); ?></td>
                </tr>
                <?php
            }
            if ($tab_consos[$ndi]['duree_international'] > '0') {
                ?>
                <tr >
                    <td width="260" align="left">International</td>
                    <td width="100" align="left"><?php echo $tab_consos[$ndi]['duree_international']; ?></td>
                    <td width="100" align="left"><?php echo $tab_consos[$ndi]['duree_international']; ?></td>
                    <td width="100" align="right"><?php echo number_format($tab_consos[$ndi]['cout_reel_international'], 2); ?></td>
                </tr>
                <?php
            }
            if ($tab_consos[$ndi]['duree_autre'] > '0') {
                ?>
                <tr >
                    <td width="260" align="left">Numéros spéciaux</td>
                    <td width="100" align="left"><?php echo $tab_consos[$ndi]['duree_autre']; ?></td>
                    <td width="100" align="left"><?php echo $tab_consos[$ndi]['duree_autre']; ?></td>
                    <td width="100" align="right"><?php echo number_format($tab_consos[$ndi]['cout_reel_autre'], 2); ?></td>
                </tr>
                <?php
            }
            ?>
            <?php
        }
        // boucle des forfaits 
        for ($l = 0; $l < count($tab_forfait[$ndi]['nom']); $l++) {
            // si SDA
            if (!empty($data['sda'][$l]) and $data['sda'][$l] <> $ndi) {
                ?>
                <tr>
                    <td>
                        <br />
                        SDA: <?php echo $data['sda'][$l]; ?> 
                    </td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
                <?php
            }
            ?>
            <tr>
                <td>
                    <?php echo $data['nom'][$l]; ?> 
                    <?php
                    //si date de souscription : mois précédent 
                    if ($date_souscription != '' and $tab_forfait[$ndi]['facturee'] != '1') {
                        $temp1             = explode("/", $data['date_souscription'][$l]);
                        $date_souscription = strtotime($temp1[1] . '/' . $temp1[0] . '/' . $temp1[2]);
                        if (date("m/Y", $date_souscription) == date("m/Y", $debut_mois_prec)) {
                            ?>
                            <small><br />Souscription au <?php echo date("d/m/Y", $date_souscription); ?></small>
                            <?php
                        }
                    }
                    ?>
                </td>
                <td></td><td></td>
                <td align="right"><?php echo $data['prix_total'][$l]; ?></td>
            </tr>
            <?php
            //si date de souscription : mois précédent 
            if ($date_souscription != '' and $tab_forfait[$ndi]['facturee'] != '1') {
                $temp1             = explode("/", $data['date_souscription'][$l]);
                $date_souscription = strtotime($temp1[1] . '/' . $temp1[0] . '/' . $temp1[2]);
                if (date("m/Y", $date_souscription) == date("m/Y", $debut_mois_prec)) {
                    ?>
                    <tr >
                        <td width="260" align="left"><?php echo $data['nom'][$l] . " - " . date("m/Y"); ?> </td>
                        <td width="100" align="left"></td>
                        <td width="100" align="left"></td>
                        <td width="100" align="right"><?php echo $data['prix_total_mois_complet'][$l]; ?></td>
                    </tr>
                    <?php
                }
            }
            ?>
            <?php
            if (!empty($data['sda'][$l]) and $data['sda'][$l] <> $ndi and $tab_consos[$data['sda'][$l]]['fax_to_mail'] == '0') {
                ?>
                <?php
                //Affichage des consos des sda
                if ($tab_consos[$data['sda'][$l]]['duree_fixe'] > '0') {
                    ?>
                    <tr >
                        <td width="260" align="left">Fixe</td>
                        <td width="100" align="left"><?php echo $tab_consos[$data['sda'][$l]]['duree_fixe']; ?></td>
                        <td width="100" align="left"><?php echo $tab_consos[$data['sda'][$l]]['hors_forfait_fixe']; ?></td>
                        <td width="100" align="right"><?php echo number_format($tab_consos[$data['sda'][$l]]['cout_reel_fixe'], 2); ?></td>
                    </tr>
                    <?php
                }
                if ($tab_consos[$data['sda'][$l]]['duree_mobile'] > '0') {
                    ?>
                    <tr >
                        <td width="260" align="left">Mobile</td>
                        <td width="100" align="left"><?php echo $tab_consos[$data['sda'][$l]]['duree_mobile']; ?></td>
                        <td width="100" align="left"><?php echo $tab_consos[$data['sda'][$l]]['hors_forfait_mobile']; ?></td>
                        <td width="100" align="right"><?php echo number_format($tab_consos[$data['sda'][$l]]['cout_reel_mobile'], 2); ?></td>
                    </tr>
                    <?php
                }
                if ($tab_consos[$ndi]['duree_international'] > '0') {
                    ?>
                    <tr >
                        <td width="260" align="left">International</td>
                        <td width="100" align="left"><?php echo $tab_consos[$data['sda'][$l]]['duree_international']; ?></td>
                        <td width="100" align="left"><?php echo $tab_consos[$data['sda'][$l]]['duree_international']; ?></td>
                        <td width="100" align="right"><?php echo number_format($tab_consos[$data['sda'][$l]]['cout_reel_international'], 2); ?></td>
                    </tr>
                    <?php
                }
                if ($tab_consos[$data['sda'][$l]]['duree_autre'] > '0') {
                    ?>
                    <tr >
                        <td width="260" align="left">Numéros spéciaux</td>
                        <td width="100" align="left"><?php echo $tab_consos[$data['sda'][$l]]['duree_autre']; ?></td>
                        <td width="100" align="left"><?php echo $tab_consos[$data['sda'][$l]]['duree_autre']; ?></td>
                        <td width="100" align="right"><?php echo number_format($tab_consos[$data['sda'][$l]]['cout_reel_autre'], 2); ?></td>
                    </tr>
                    <?php
                }
                ?>
                <?php
            }
            ?>

            <?php
        }
        //Affichage des consos du fax
        if ($tab_consos[$ndi]['fax_to_mail'] != '0') {
            ?>       
            <tr>
                <td>Nombre de pages envoyées</td>
                <td><?php echo $tab_consos['fax']['nb']; ?></td>
                <td></td>
                <td align="right"><?php echo number_format($tab_consos['fax']['cout_reel'], 2); ?></td>
            </tr>
            <?php
        }
        ?>
    </table>

    <br />
    <table width='560'>		
        <tr><td align='right' width='260'><b>Total services et consommations HT</b></td>
            <td align='right' width='100'></td>
            <td align='center' width='100'></td>
            <?php
            //Affichage du total de la ligne
            if ($tab_consos[$ndi]['fax_to_mail'] != '0') {
                ?>
                <td align='right' width='100'><?php echo "<b>" . number_format($tab_forfait[$ndi]['total_ligne'] + $tab_consos[$ndi]['cout_reel'] + $tab_consos['fax']['cout_reel'], 2) . "</b>"; ?></td>
                <?php
            } else {
                ?>
                <td align='right' width='100'><?php echo "<b>" . number_format($tab_forfait[$ndi]['total_ligne'] + $tab_consos[$ndi]['cout_reel'], 2) . "</b>"; ?></td>    
                <?php
            }
            ?>
        </tr>
    </table>

    <hr />
    <?php
//mise en facturee dans la base des forfaits / options pour éviter une refacturation au prorata
    $requete = $bdd->prepare("UPDATE `detail_client` SET facturee = '1' WHERE ndi = '" . $ndi . "' ");
//}
    $requete->execute() or exit(print_r($bdd->errorInfo()));
}
if ($tab_consos['num_inconnu']['duree'] != 0) {
    ?>		
    <br />
    <table width="560" align="left" >

        <tr >
            <td width="260" align="left"><b>Consommations appellant inconnus</b></td>
            <td width="100" align="left"><b>Durée</b></td>
            <td width="100" align="left"><b>Hors Forfait</b></td>
            <td width="100" align="center"><b>Montant € HT</b></td>
        </tr>

        <tr>
            <td></td>
            <td><?php echo "<b>" . $tab_consos['num_inconnu']['duree'] . "</b>"; ?></td>
            <td></td>
            <td align="center"><?php echo "<b>" . number_format($tab_consos['num_inconnu']['cout_reel'], 2) . "</b>"; ?></td>
        </tr>

    </table>
    <?php
}
?>  
